// editnotbe.js
var util = require('../../utils/util.js')
var glo = require('../../utils/global.js')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selContents: [],
    selIndexs: [],
    selChapters: [],
    userInfo: [],
    edit: 0,
    screenH: 0,
    read: "",
    notbeid: "",
    hasSumit: false,
    evaContent: '',
    isalert: false,
    autoHeight: false,
    djgs: decodeURI("%E5%86%99%E7%82%B9%E4%BD%A0%E7%9A%84%E8%AF%BB%E7%BB%8F%E6%84%9F%E5%8F%97%E5%90%A7,300%E5%AD%97%E5%86%85...")
  },
  openAlert: function() {
    var bool = this.data.isalert;
    this.setData({
      isalert: !bool
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var screenH = 0;
    var selTit = options.tit || '';
    var selContents = JSON.parse(options.content);
    var selIndexs = JSON.parse(options.index);
    var selChapters = JSON.parse(options.chapter);
    var notbeid = options.notbeid;
    var edit = parseInt(options.edit); //0表示添加，1表示编辑，2表示查看
    var notbecontent = options.notbecontent;
    console.log(selContents)
    // console.debug("传过来的:" + notbecontent);
    var notbedate = options.notbedate;
    var editnotbe = "";
    var readnotbe = "readnotbe";
    if (edit == 1) {
      editnotbe = "";
      readnotbe = "readnotbe";
      notbedate = "";
      wx.setNavigationBarTitle({
        title: "编辑" + decodeURI("%E7%AC%94%E8%AE%B0") //页面标题
      })
    } else if (edit == 2) { 
      editnotbe = "readnotbe";
      readnotbe = "";
      wx.setNavigationBarTitle({
        title: decodeURI("%E7%AC%94%E8%AE%B0")//页面标题
      })
    } else { //
      notbeid = "";
      notbecontent = "";
      notbedate = "";
      wx.setNavigationBarTitle({
        title: "添加" + decodeURI("%E7%AC%94%E8%AE%B0") //页面标题
      })
    }
    that.data.evaContent = notbecontent;
    that.setData({
      edit: edit,
      editnotbe: editnotbe,
      readnotbe: readnotbe,
      notbedate: notbedate
    });
    wx.getSystemInfo({
      success: function(res) {

        screenH = res.windowHeight

      }
    });
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo) {
      //更新数据
      that.setData({
        userInfo: userInfo
      });
    });
    if (edit == 2) {
      screenH = screenH - 100;
    }
    this.setData({
      selTit: selTit,
      selContents: selContents,
      selIndexs: selIndexs,
      selChapters: selChapters,
      screenH: screenH,
      notbeid: notbeid
    });
    console.debug("问题在这吗？:" + that.data.evaContent);
  },
  /*
  //事件
  textBlur: function (e) {
    if (e.detail && e.detail.value.length > 0) {
      if (e.detail.value.length < 12 || e.detail.value.length > 500) {
        //app.func.showToast('内容为12-500个字符','loading',1200);
      } else {
        this.setData({
          evaContent: e.detail.value
        });
      }
    } else {
      this.setData({
        evaContent: ''
      });
     console.debug("问题在这吗？");
    }
  },*/
  delnotbe: function() {
    var that = this;
    var notbeid = this.data.notbeid;
    wx.showModal({
      title: '提示',
      content: '确定删除该条' + decodeURI("%E7%AC%94%E8%AE%B0")+"?",
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: glo.https + '/person/notedel?noteid=' + notbeid,
            success: function(res) {
              if (res.data == true) {
                var userInfo = that.data.userInfo;
                for (var i = 0; i < userInfo.notes.length; i++) {
                  if (userInfo.notes[i].noteId == notbeid) {
                    userInfo.notes.splice(i, 1);
                    app.setUserInfo(userInfo);
                    wx.navigateBack({

                    })
                  }
                }
              }
            }
          });
        }
      }
    });
  },
  lineChange: function(e) {
    console.log(e.detail);
    let that = this;
    if (e.detail.height > 130) {
      that.setData({
        autoHeight: true
      });
    }
  },
  //提交事件
  evaSubmit: function(eee) {
    var that = this;
    var date = util.getNowFormatDate();
    var tit = eee.detail.value.evatit;
    var content = eee.detail.value.evaContent;
    var appUserId = that.data.userInfo.userId;
    var selVers = that.data.selContents;
    var selIndexs = that.data.selIndexs;
    var selChapters = that.data.selChapters;
    var hasSumit = that.data.hasSumit;
    var edit = that.data.edit;
    var notbeid = that.data.notbeid;
    if (tit == "") {
      tit = "无标题";
    }
    if (content.length == 0) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'success',
        duration: 1000
      })
      return;
    }
    hasSumit = true;
    this.setData({
      hasSumit: hasSumit
    });
    console.log(edit);
    if (edit == 0) {
      wx.showLoading({
        title: "正在提交...",
        mask: true
      });
      wx.request({
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        },
        url: glo.https + '/person/noteadd1',
        data: {
          selchapters: selChapters,
          userid: appUserId,
          notedate: date,
          content: encodeURI(content),
          notetitle: encodeURI(tit)
        },
        success: function(res2) {
          if (res2.data == true) {
            wx.showModal({
              title: '提示',
              content: decodeURI("%E7%AC%94%E8%AE%B0")+'保存成功',
              showCancel: false, //不显示取消按钮
              confirmText: '确定',
              success: function(res) {
                if (res.confirm) {
                  that.setUsernotbes();
                }
              }

            })
          } else {
            that.setData({
              hasSumit: false
            });
          }
          wx.hideLoading();
        },
        fail: function(res3) {
          console.log(data)
          that.setData({
            hasSumit: false
          });
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: decodeURI("%E7%AC%94%E8%AE%B0")+'保存失败',
            showCancel: false, //不显示取消按钮
            confirmText: '确定'
          })
        }
      });
    } else if (edit == 1) {
      wx.showLoading({
        title: "正在提交...",
      });
      wx.request({
        url: glo.https + '/person/noteedit1',
        data: {
          noteid: notbeid,
          selchapters: selChapters,
          userid: appUserId,
          notedate: date,
          content: encodeURI(content),
          notetitle: encodeURI(tit)
        },
        success: function(res2) {
          if (res2.data == true) {
            wx.showToast({
              title: decodeURI("%E7%AC%94%E8%AE%B0")+'修改完成',
              duration: 2000
            });

            that.setUsernotbes();

          } else {
            that.setData({
              hasSumit: false
            });
            wx.hideLoading();
          }

        },
        fail: function(res3) {
          console.log(res3);
          that.setData({
            hasSumit: false
          });
          wx.hideLoading();
        }
      });
    }

  },

  setUsernotbes: function() {
    var that = this;
    var userInfo = that.data.userInfo;
    wx.request({
      url: glo.https + '/person/getnotes',
      data: {
        userid: userInfo.userId
      },
      success: function(res2) {
        console.log(res2);
        userInfo.notes = res2.data;
        app.setUserInfo(userInfo);
        setTimeout(function() {
          wx.navigateBack({});
        }, 1000);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

    var notbecontent = this.data.evaContent;
    console.debug("onready:" + notbecontent)
    this.setData({
      evaContent: notbecontent
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    var notbecontent = this.data.evaContent;
    console.debug("onshow:" + notbecontent)
    this.setData({
      evaContent: notbecontent
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function() {
    var v = "菜单";
    if (this.data.pageEnable) {
      v = "%E5%9C%A3%E7%BB%8F"
      v = decodeURI(v)
    }
    return {

      title: ' 这是一款免下载的小程序' + v + ',点开即可马上使用！',

      desc: '微信小程序版本' + v,

      path: '/pages/index/index'

    }
  }
})